package com.eshop.modules.template.service.impl;

import java.io.IOException;
import java.util.Iterator;
import com.eshop.utils.FileUtil;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.eshop.common.utils.QueryHelpPlus;
import com.eshop.modules.template.service.dto.ShippingTemplatesRegionDto;
import java.util.LinkedHashMap;
import java.util.List;
import com.github.pagehelper.PageInfo;
import java.util.Map;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.template.service.dto.ShippingTemplatesRegionQueryCriteria;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.template.service.ShippingTemplatesRegionService;
import com.eshop.modules.template.domain.ShippingTemplatesRegion;
import com.eshop.modules.template.service.mapper.ShippingTemplatesRegionMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class ShippingTemplatesRegionServiceImpl extends BaseServiceImpl<ShippingTemplatesRegionMapper, ShippingTemplatesRegion> implements ShippingTemplatesRegionService
{
    private final IGenerator generator;
    
    public Map<String, Object> queryAll(final ShippingTemplatesRegionQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<ShippingTemplatesRegion> page = new PageInfo(this.queryAll(criteria));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", this.generator.convert(page.getList(), ShippingTemplatesRegionDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }
    
    public List<ShippingTemplatesRegion> queryAll(final ShippingTemplatesRegionQueryCriteria criteria) {
        return (this.baseMapper).selectList(QueryHelpPlus.getPredicate(ShippingTemplatesRegion.class, criteria));
    }
    
    public void download(final List<ShippingTemplatesRegionDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final ShippingTemplatesRegionDto yxShippingTemplatesRegion : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("省ID", yxShippingTemplatesRegion.getProvinceId());
            map.put("模板ID", yxShippingTemplatesRegion.getTempId());
            map.put("城市ID", yxShippingTemplatesRegion.getCityId());
            map.put("首件", yxShippingTemplatesRegion.getFirst());
            map.put("首件运费", yxShippingTemplatesRegion.getFirstPrice());
            map.put("续件", yxShippingTemplatesRegion.getContinues());
            map.put("续件运费", yxShippingTemplatesRegion.getContinuePrice());
            map.put("计费方式", yxShippingTemplatesRegion.getType());
            map.put("分组唯一值", yxShippingTemplatesRegion.getUniqid());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
    
    public ShippingTemplatesRegionServiceImpl(final IGenerator generator) {
        this.generator = generator;
    }
}
